package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.entity.Result;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws Exception {
        String orderDate = (String) map.get("orderDate");

        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断用户是否在重复预约(通过用户的id 套餐 和 时间来确定用户是否是重复预约)
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        System.out.println(member);
        //如果用户不为空,去除用的的id 套餐id 和 时间来确定
        if (member != null) {
            //会员id
            Integer id = member.getId();
            //预约日期
            Date order_date = DateUtils.parseString2Date(orderDate);
            //体检套餐id
            String setmealId = (String) map.get("setmealId");

            Order order = new Order(id, order_date, Integer.parseInt(setmealId));
            List<Order> orders = orderDao.findByCondition(order);

            //如果list不为空说明用户在重复预约
            if (orders != null && orders.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }


        }

        if(member == null){
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            System.out.println("hello...");
            memberDao.add(member);//自动完成会员注册
        }



        //预约成功通知,将预约信息保存到数据库
        Order order = new Order();
        order.setMemberId(member.getId());//会员id
        order.setOrderDate( DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//预约状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));


        orderDao.add(order);

        //修改数据库中已预约的人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);



        //返回预约对象
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map!=null){
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map ;
    }
}
